/**
 * &copy; 2006 NewInstance.it
 */
package it.newinstance.jrainbow.source;



/**
 * @author Luigi R. Viggiano
 * @version $Id: TaggedSource.java 138 2007-06-12 12:23:29Z luigi.viggiano $
 */
public class TaggedSource {
    private static enum Status { CREATED, PARSING, PARSED }
    private Status status = Status.CREATED;
    private TaggedChar[] taggedChars;
    private long elapsed;
    private long parseStartTime;
    private long parseEndTime;

    /**
     * @param source    the source code
     */
    public TaggedSource(String source) {
        taggedChars = new TaggedChar[source.length()];
        for (int i = 0; i < source.length(); i++) { 
            taggedChars[i] = new TaggedChar(source.charAt(i));
            if (i > 0)
                taggedChars[i].setPrevious(taggedChars[i-1]);
        }
    }
               
    public void mark(int start, int length, Tag tag) {
        for (int i = 0; i < length; i++)
            taggedChars[start + i].setTag(tag);
    }
    
    public void startParse() {        
        if (status == Status.PARSING) 
            throw new IllegalStateException("Parsing already started");
        parseStartTime = System.currentTimeMillis();
        status = Status.PARSING;
    }
    
    public void endParse() {
        if (status != Status.PARSING) 
            throw new IllegalStateException("Parsing has not started");
        parseEndTime = System.currentTimeMillis();
        elapsed = parseEndTime - parseStartTime;
        status = Status.PARSED;
    }

    public long getElapsed() {
        return elapsed;
    }

    public long getParseStartTime() {
        return parseStartTime;
    }
    
    public long getParseEndTime() {
        return parseEndTime;
    }

	void accept(AbstractIterator<?> iterator) {
		iterator.taggedChars = taggedChars;
	}
}
